package com.fuxin.hanfu.controller;

import com.fuxin.hanfu.domain.Cart;

import com.fuxin.hanfu.service.CartService;

import com.fuxin.hanfu.uitls.MyResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;
    @GetMapping("/list")
    public List<Cart> findList(){
        return cartService.findList();
    }

 @GetMapping("/find/{id}") // 修改路径，使其更简洁
 public ResponseEntity<MyResponse> findById(@PathVariable Integer id) {
     try {
         Cart cart = cartService.findById(id);
         return ResponseEntity.ok(MyResponse.toJson(true, cart));
     } catch (Exception e) {
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(MyResponse.toJson(false, "获取购物车失败"));
     }
 }
 /*添加*/
    @PostMapping("/add")
    public ResponseEntity<MyResponse> insert(@RequestBody Cart cart) {
        try {
            cartService.insert(cart);
            return ResponseEntity.ok(MyResponse.toJson(true, "添加成功"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(MyResponse.toJson(false, "添加购物车失败"));
        }
    }
/*修改*/
  @PutMapping("/update")
  public ResponseEntity<MyResponse> update(@RequestBody Cart cart) {
      try {
          cartService.update(cart);
          return ResponseEntity.ok(MyResponse.toJson(true, "更新成功"));
      } catch (Exception e) {
          return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(MyResponse.toJson(false, "更新购物车失败"));
      }
  }
/*删除*/
    @DeleteMapping("/delete/{id}") // 修改路径，使其更简洁
    public ResponseEntity<MyResponse> delete(@PathVariable Integer id) {
        try {
            cartService.delete(id);
            return ResponseEntity.ok(MyResponse.toJson(true, "删除成功"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(MyResponse.toJson(false, "删除购物车失败"));
        }
    }
}
